#include "rpc/rpc_env.h"

#include <signal.h>
#include <sys/resource.h>

DEFINE_int32(max_open_files, 10240, "max number of files can be opened");
#define MIN_FILE_NUM 1024

namespace {
bool SetMaxOpenFiles(int32 num);
}

namespace rpc {

bool Init() {
  ::signal(SIGPIPE, SIG_IGN);
  if (!SetMaxOpenFiles(FLAGS_max_open_files)) {
    return false;
  }

  return true;
}

}

namespace {
bool SetMaxOpenFiles(int32 num) {
  num = std::max(num, MIN_FILE_NUM);
  rlimit lim = { num, num };

  int32 ret = ::setrlimit(RLIMIT_NOFILE, &lim);
  if (ret != 0) {
    PLOG(WARNING)<< "setrlimit error";
    return false;
  }

  return true;
}
}
